#include<iostream>
#include<stdio.h>
#include<math.h>
//#include "stdafx.h"



class Player
{
	public:
	int amount;  
	int ct;
	Player()
	{
		amount=0;
		ct=0;
	}
	int giveMoney()
	{
		if(amount==0) return -1; 
		int current=pow(2,ct);
		if(current>amount)
		{
			int temp=amount;
			amount=0;
			return temp;
		}
		else
		{
			amount=amount-current;
			ct++;
			return current; 
		}
	}
	int takeMoney(int money)
	{
		if(amount==0) return -1; 
		amount+=money; 
	}
};


void playGame(int no_of_player, int initial_amt)
{
	Player *player;
	player=new Player[no_of_player];
	for(int i=0;i<no_of_player;i++)
		player[i].amount=initial_amt;

	int playersLeft=no_of_player;
	int currentPlayer=0;
	int playerLastRecieved=0;
	while(playersLeft)
	{
		int i=(currentPlayer+1)>=no_of_player?0:(currentPlayer+1);
		int _flag=0;
		printf("Current Player :%d\n",currentPlayer);
		while(player[currentPlayer].amount>0)
		{
			if(i!=currentPlayer)
			{
				if(player[i].amount>0)
				{
					int amt=player[currentPlayer].giveMoney();
					if(amt==-1) break;

					printf("\tPlayer %d: %d\n",i,amt); 

					player[i].takeMoney(amt);
					playerLastRecieved=i;
				}
			}
			else
			{
				_flag=1;
				break;
			}
			i++;
			if(i>=no_of_player) i=0;
		}
		if(_flag==1) break;
		currentPlayer=playerLastRecieved;
		playersLeft--;
	}
	/*Console::Write(player[currentPlayer].amount);*/
//	cin.get();
}
int _tmain()
{
	playGame(10,10);	
	return 0;
}
